/*
 * 作者: 侯晓宇
 * 时间: 2017-01-24
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {
	require("jquery");

	var c = require('app/utils/CommonUtils');
	var hc = require('app/utils/HttpClientUtils');
	var EventUtils = require('app/utils/EventUtils');
	var tip = require('app/utils/TipUtils');
	var appInfo = require('app/AppInfo');
	var WeixinUtils = require('app/utils/WeixinUtils');
	var win = window;
	
	
	var action = {};

	action.back = function() {
	
		api.closeWin({
        });
	}
	action.save = function(){
		var room = {};
		EventUtils.gatherFrmVal('CreateRoomCoverFrm')
		.then(function(data){ //第一个Frame
		
			room.coverurl = data;
			return EventUtils.gatherFrmVal('CreateRoomSimpleFrm');
		})
		.then(function(data){ //第二个窗体CreateRoomFrm返回值
			
			room = $.extend({},room,data);
		})
		.then(function(){ //上传url
//			{"coverurl":"/var/mobile/Containers/Data/Application/F8EA2F3E-4D40-428B-BBDB-5E402CC1769D/Documents/uzfs/A6937348585202/imageClip/result.png","userid":"b69a5be1-2b74-4118-a3bf-72ab8b98799a","name":"g g g g","starttime":1486441179085,"live_type":"2","des":"","room_type":"0"}
			if(!c.isNullStr(room.coverurl)){
				return hc.uploadImg(room.coverurl, null,'img');
			}else {
				return null;
			}
		})
		.then(function(resp){ // 图片上传成功
		
			
			if(c.isNullStr(resp)){
				room.coverurl = null;
			}else{
				if(resp.status == "201"){//上传成功
					room.coverurl = resp.result.path;
				}
			}
			
			c.l(room);
			if(c.isNullStr(room.name)){
				tip.show("请添加主题");
				return;
			}
			
			var url = appInfo.serverbase + "/luyan/room/create";
			var body = room;
//			{"coverurl":"/attachment/default/c851864d-f7ec-401a-bcea-96021f136cbe","userid":"b69a5be1-2b74-4118-a3bf-72ab8b98799a","name":"啊啊啊啊啊","starttime":1486793460000,"live_type":"2","des":"","room_type":"0"}
			return hc.post(url,body);
		})
		.then(function(resp){
			if(c.isNull(resp)){
				return;
			}
			if(resp.status == "201"){//创建成功
				
				c.l(room);
				var sharetype = 0;
				if(room.webchat == "wechat"){
					sharetype = 1;
				}else if(room.webchat == "circle"){
					sharetype = 2;
				}
				
				WeixinUtils.share(sharetype, room.name, resp.result.room.uuid, function(){
					alert("分享成功");
					
					//进入RoomWIn页面, 并传入参数room
					api.openWin({
			            name: 'RoomWin',
			            url: './RoomWin.html',
			            pageParam: resp.result.room
		            });
		            
		            
		            setTimeout(function(){
			            api.closeWin({
	        			});
		            }, 2000);
				});

				
	            
					
			}
		});	
	}
	
	return action;
});